<?php
class ModelCatalogOnline extends Model {
	public function addOnline($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "online SET nickname = '" . $this->db->escape($data['nickname']) . "', image='".(int)$data['image']."', sort_order = '" . (int)$data['sort_order'] . "', status = '" . (int)$data['status'] . "', phone='".$this->db->escape($data['phone'])."'");
	
		$online_id = $this->db->getLastId();
		
		
		foreach ($data['online_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "online_description SET online_id = '" . (int)$online_id . "', language_id = '" . (int)$language_id . "', description = '" . $this->db->escape($value['description']) . "'");
		}
		
		
		
		
	}
	
	public function editOnline($online_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "online SET nickname = '" . $this->db->escape($data['nickname']) . "', image='".(int)$data['image']."', sort_order = '" . (int)$data['sort_order'] . "', status = '" . (int)$data['status'] . "', phone='".$this->db->escape($data['phone'])."' WHERE online_id='".(int)$online_id."'");
	
		
		$this->db->query("DELETE FROM ".DB_PREFIX."online_description WHERE online_id='".(int)$online_id."'");
		
		foreach ($data['online_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "online_description SET online_id = '" . (int)$online_id . "', language_id = '" . (int)$language_id . "', description = '" . $this->db->escape($value['description']) . "'");
		}
		
		
		
		
		
	}
	
	public function deleteOnline($online_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "online WHERE online_id = '" . (int)$online_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "online_description WHERE online_id = '" . (int)$online_id . "'");		
		
	} 

	public function getOnline($online_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "online WHERE online_id = '" . (int)$online_id . "'");
		
		return $query->row;
	} 
	
	public function getOnlines($data = array()) {
		
		
		
		
		$online_data=array();
		
		if (!$online_data) {
			$sql = "SELECT p.online_id  FROM " . DB_PREFIX . "online p LEFT JOIN " . DB_PREFIX . "online_description pd ON (p.online_id = pd.online_id) "; 
						
			
			$sql .= " WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1'"; 
			
			if (!empty($data['filter_description'])) {
				$sql .= " AND (";
											
				if (!empty($data['filter_description'])) {
					$implode = array();
					
					$words = explode(' ', $data['filter_description']);
					
					foreach ($words as $word) {
						if (!empty($data['filter_description'])) {
							$implode[] = "LCASE(pd.description) LIKE '%" . $this->db->escape(utf8_strtolower($word)) . "%' ";
						} 			
					}
					
					if ($implode) {
						$sql .= " " . implode(" OR ", $implode) . "";
					}
				}
				
			
				
				
				
				$sql .= ")";
			}
			
			if (!empty($data['filter_phone'])) {
				$sql .= " AND LCASE(p.phone) LIKE '%" . $this->db->escape(utf8_strtolower($data['filter_phone'])) . "%'";
			}
			
			if (!empty($data['filter_nickname'])) {
				$sql .= " AND LCASE(p.nickname) LIKE '%" . $this->db->escape(utf8_strtolower($data['filter_nickname'])) . "%'";
			}

			
			
			
			
			if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
				$sql .= " AND p.status = '" . (int)$data['filter_status'] . "'";
			}
			
			$sql .= " GROUP BY p.online_id";
			
			$sort_data = array(
				'pd.description',
				'p.phone',
				'p.nickname',
				'p.status',				
				'p.sort_order'				
			);	
			
			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				if ($data['sort'] == 'pd.description' || $data['sort'] == 'p.phone' || $data['sort'] == 'p.nickname') {
					$sql .= " ORDER BY LCASE(" . $data['sort'] . ")";
				} else {
					$sql .= " ORDER BY " . $data['sort'];
				}
			} else {
				$sql .= " ORDER BY p.sort_order";	
			}
			
			if (isset($data['order']) && ($data['order'] == 'ASC')) {
				$sql .= " ASC";
			} else {
				$sql .= " DESC";
			}
		
			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}				
	
				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}	
			
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}
			
			$online_data = array();
					
			$query = $this->db->query($sql);
		
			foreach ($query->rows as $result) {
				$online_data[$result['online_id']] = $this->getOnline($result['online_id']);
			}
			
			
		}
		
		return $online_data;
	}	
	
	
	public function getOnlineDescriptions($online_id) {
		$online_description_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "online_description WHERE online_id = '" . (int)$online_id . "'");
		
		foreach ($query->rows as $result) {
			$online_description_data[$result['language_id']] = array(				
				'description'      => $result['description']
			);
		}
		
		return $online_description_data;
	}		
	
	
		
	public function getTotalOnlines($data = array()) {
		$sql = "SELECT COUNT(DISTINCT p.online_id) AS total FROM " . DB_PREFIX . "online p LEFT JOIN " . DB_PREFIX . "online_description pd ON (p.online_id = pd.online_id) ";

		
					
		$sql .= " WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' ";
		
		if (!empty($data['filter_description']) ) {
			$sql .= " AND (";
								
			if (!empty($data['filter_description'])) {
				$implode = array();
				
				$words = explode(' ', $data['filter_description']);
				
				foreach ($words as $word) {
					if (!empty($data['filter_description'])) {
						$implode[] = "LCASE(pd.description) LIKE '%" . $this->db->escape(utf8_strtolower($word)) . "%'";
					}			
				}
				
				if ($implode) {
					$sql .= " " . implode(" OR ", $implode) . "";
				}
			}
			
			
		
			$sql .= ")";
		}
		
			
		
		if (!empty($data['filter_phone'])) {
			$sql .= " AND p.phone LIKE '%" . $this->db->escape($data['filter_phone']) . "%'";
		}
		
		if (!empty($data['filter_status'])) {
			$sql .= " AND p.status = '" . (int)$data['filter_status'] . "'";
		}
		
		if (!empty($data['filter_nickname'])) {
			$sql .= " AND p.nickname LIKE  '%" . $this->db->escape($data['filter_nickname']) . "%'";
		}
		
		$query = $this->db->query($sql);
		
		return $query->row['total'];
	}
		
	
}
?>